package com.rui.shangXueTang.clue;

/**
 * @author : [锐神NO.3]
 * @version : [v1.0]
 * @className : HeroNode
 * @createTime : [2022/2/17 15:54]
 * @description : [描述该类的功能]
 */

@SuppressWarnings("all")
public class HeroNode {

    private int no;

    private String name;

    private HeroNode left;

    private HeroNode right;

    /**
     * 如果是0，表示指向的是左子树  1表示指向前驱
     */
    private int noLeft;

    /**
     * 如果是0，表示指向的是右子树  1表示指向后继
     */
    private int noRight;

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    public int getNoLeft() {
        return noLeft;
    }

    public void setNoLeft(int noLeft) {
        this.noLeft = noLeft;
    }

    public int getNoRight() {
        return noRight;
    }

    public void setNoRight(int noRight) {
        this.noRight = noRight;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    /**
     * 删除节点
     * 删除的可能是叶子节点，也可能是非叶子节点
     */
    public void delNode(int no){
        //1.如果当前节点的左节点不为空，并且左子节点正好是要删除的节点
        if(this.left != null && this.left.no == no){
            this.left = null;
            return;
        }

        //2.如果当前节点的右节点不为空，并且右子节点正好是要删除的节点
        if(this.right != null && this.right.no == no){
            this.right = null;
            return;
        }

        //3.如果1和2都没有执行，则左递归
        if(this.left != null){
            this.left.delNode(no);
        }

        //4.如果3没有执行，则右递归
        if(this.right != null){
            this.right.delNode(no);
        }
    }

    /**
     * 前序遍历所有
     */
    public void preSelect(){
        System.out.println(this);

        //左递归
        if(this.left != null){
            this.left.preSelect();
        }

        //右递归
        if(this.right != null){
            this.right.preSelect();
        }
    }

    /**
     * 中序遍历
     */
    public void midSelect(){
        //左递归
        if(this.left != null){
            this.left.midSelect();
        }

        System.out.println(this);

        //右递归
        if(this.right != null){
            this.right.midSelect();
        }
    }

    /**
     * 后序遍历
     */
    public void backSelect(){
        //左递归
        if(this.left != null){
            this.left.backSelect();
        }

        //右递归
        if(this.right != null){
            this.right.backSelect();
        }

        System.out.println(this);
    }

    /**
     * 根据编号前序遍历查找
     */
    public HeroNode preSelect(int no){
        if(this.no == no){
            return this;
        }

        HeroNode temp = null;

        //左递归查找
        if(this.left != null){
            temp = this.left.preSelect(no);
        }

        if(temp != null){
            return temp;
        }

        //右递归查找
        if(this.right != null){
            temp = this.right.preSelect(no);
        }

        return temp;
    }
}
